package com.leetcode.partition14;

/**
 * @author rkc
 * @date 2022/8/31 9:07
 */
public class LC1360日期之间隔几天 {

    private final int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public int daysBetweenDates(String date1, String date2) {
        if (date1.compareTo(date2) > 0) {
            String t = date1;
            date1 = date2;
            date2 = t;
        }
        String[] ss1 = date1.split("-"), ss2 = date2.split("-");
        int y1 = Integer.parseInt(ss1[0]), m1 = Integer.parseInt(ss1[1]), d1 = Integer.parseInt(ss1[2]);
        int y2 = Integer.parseInt(ss2[0]), m2 = Integer.parseInt(ss2[1]), d2 = Integer.parseInt(ss2[2]);
        int ans = 0;
        while (y1 != y2 || m1 != m2 || d1 != d2) {
            ans++;
            d1++;
            if (d1 > get(y1, m1)) {
                d1 = 1;
                m1++;
                if (m1 > 12) {
                    m1 = 1;
                    y1++;
                }
            }
        }
        return ans;
    }

    private int get(int y, int m) {
        if (m == 2) {
            return months[m] + (check(y) ? 1 : 0);
        }
        return months[m];
    }

    private boolean check(int y) {
        return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
    }
}
